import {
  getTokenList,
  auditToken,
  autoStatus,
  setAudit,
  setFeeInfo,
  getParallelList,
  getFeeTokens
} from "./Token.service";
export default {
  data() {
    return {
      loading: false,
      manualAudit: false,
      showFeeDialog: false,
      createToken: {},
      search: "",
      chainList: [],
      statusList: [{
          text: "全部",
          value: null
        },
        {
          text: '待审核',
          value: 5
        }, {
          text: '允许发行(人工)',
          value: 6
        }, {
          text: '拒绝发行(人工)',
          value: 7
        }, {
          text: '发行成功',
          value: 8
        }, {
          text: '发行失败',
          value: 9
        }
      ],
      param: {
        chain_id: "",
        status: null,
        page: 1,
        size: 10,
        total: 0,
        owner: ""
      },
      feeParam: {
        chains: [],
        chainName: "",
        platform_id: '',
        tokens: [],
        charge_unit: "",
        issue_charge: "",
        index: ''
      },
      tableData: []
    };
  },
  watch: {
    "feeParam.issue_charge": {
      handler(newVal, oldVal) {
        let index = newVal.indexOf('.')
        if (index > -1) {
          let n = newVal.substring(0, index + 1)
          let m = newVal.substring(index + 1, newVal.length)
          this.feeParam.issue_charge = m.length > 3 ? n + m.substring(0, 3) : n + m
        }
      },
      deep: true
    },
    showFeeDialog() {
      this.feeParam = {
        chains: [],
        chainName: "",
        platform_id: '',
        tokens: [],
        charge_unit: "",
        issue_charge: ""
      }
    }
  },
  mounted() {
    this.init()
    this.getAutoStatus()
  },
  methods: {
    async init() {
      const res = await getParallelList(this.$store.state.userInfo.platform_id)
      if (res.code === 0) {
        if (res.data.length === 1) this.param.chain_id = res.data[0].chain_id
        else this.chainList = res.data
      }
      this.searchList(this.param);
    },
    async searchList(form) {
      this.loading = true
      const data = await getTokenList(form);
      if (data.code === 0) {
        this.tableData = data.data.list;
        this.tableData.forEach(item => {
          if (item.url && item.url !== '') {
            item.url = item.url.slice(0, -15)
          } else item.url = undefined
        })
        this.param.total = data.data.page.pageCount * data.data.page.pageSize
      }
      this.loading = false
    },
    async searchAdr() {
      this.param.owner = this.search
      this.searchList(this.param)
    },
    async getAutoStatus() {
      const data = await autoStatus()
      if (data.code === 0) {
        this.manualAudit = data.data === 1
      }
    },
    async setAuditStatus(status) {
      let audit = status === true ? 'open' : 'close'
      let msg = status === true ? "打开" : "关闭"
      const data = await setAudit(audit)
      if (data.code === 0) {
        this.$notify.success({
          title: '提示',
          message: `人工审核${msg}成功`
        })
      } else {
        this.$notify.error({
          title: '提示',
          message: `人工审核${msg}失败`
        })
      }
    },
    async openFeeDialog() {
      this.showFeeDialog = true
      const res = await getFeeTokens()
      if (res.code === 0) {
        this.feeParam.chains = res.data
        if (this.feeParam.chains.length === 1) {
          this.feeParam.platform_id = this.feeParam.chains[0].platform_id
          this.feeParam.tokens = res.data[0].tokens
          this.feeParam.charge_unit = this.feeParam.chains[0].charge_unit || this.feeParam.tokens[0]
          this.feeParam.issue_charge = this.feeParam.chains[0].issue_charge
        }
      }
    },
    selectFeeChain(index) {
      let chain = this.feeParam.chains[index]
      this.feeParam.tokens = chain.tokens
      this.feeParam.charge_unit = ''
      this.feeParam.platform_id = chain.platform_id
      this.feeParam.charge_unit = chain.charge_unit
      this.feeParam.issue_charge = chain.issue_charge
      if (this.feeParam.tokens.length === 1) this.feeParam.charge_unit = this.feeParam.tokens[0]
    },
    async setFee() {
      if (Number(this.feeParam.issue_charge) <= 0 || this.feeParam.issue_charge === '' || isNaN(Number(this.feeParam.issue_charge))) {
        this.$notify.warning({
          title: '提示',
          message: '手续费数量必须大于0的数字'
        })
        return
      }
      const res = await setFeeInfo(this.feeParam)
      if (res.code === 0) {
        this.$notify.success({
          title: '提示',
          message: '设置成功'
        })
        this.searchList(this.param);
        this.showFeeDialog = false;
      }
    },
    async audit(token, status) {
      if (status === 1) {
        this.$confirm(
          `确定允许 <span class="confirm-tip-color">${token.symbol}</span> 币种发行？`,
          "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            dangerouslyUseHTMLString: true
          }
        ).then(() => {
          let param = {
            id: token.id,
            status: 6,
            msg: ""
          };
          auditToken(param).then(res => {
            if (res.code === 0) {
              this.$notify.success({
                title: '提示',
                message: "操作成功"
              })
              this.searchList(this.param);
            }
            // else {
            //   this.$notify.error({
            //     title: '提示',
            //     message: '操作失败'
            //   })
            // }
          })
        });
      } else {
        setTimeout(() => {
          document.querySelector('.defeat-reason input').setAttribute('maxlength', 30)
        }, 500)
        this.$prompt("请输入失败原因", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPlaceholder: '请输入失败理由，长度30字内。',
          customClass: 'defeat-reason'
        }).then(({
          value
        }) => {
          let param = {
            id: token.id,
            status: 7,
            msg: value || ''
          };
          auditToken(param).then(res => {
            if (res.code === 0) {
              this.$notify.success({
                title: '提示',
                message: "操作成功"
              })
              this.searchList(this.param);
            }
          })
        });
      }
    },
    filterStatus(value) {
      this.param.status === value;
      this.searchList(this.param)
    },
    filterName(id) {
      this.param.chain_id = id
      this.searchList(this.param)
    },
    // 跳转浏览器查看token
    viewToken(token) {
      let url = `${token.url}tokenDetail?TokenName=${token.symbol}`
      window.open(url)
    },
    success() {
      this.$message.success("复制成功");
    },
    error() {
      this.$message.error("复制失败");
    },
    pageSizeChange(val) {
      this.param.size = val
      this.param.page = 1
      this.searchList(this.param)
    },
    pageChange(val) {
      this.param.page = val
      this.searchList(this.param)
    }
  }
};